package generator.api;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Method to use the Java reflection and Command pattern
 * @author jchaline
 *
 * @param <T> the param to return with the given method
 */
public class Command<T> {
	
	private Method method;
	private Object[] arguments;
	
	/**
	 * the way to invok the method
	 * @param cible the object to use method
	 * @param args the arguments for the method
	 * @return the result
	 */
	@SuppressWarnings("unchecked")
	public T call(Object cible, Object...args){
		try {
			return (T) method.invoke(cible, args);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Constructor 
	 * @param m the method to pass
	 * @param args the arguments to use
	 */
	public Command(Method m, Object... args){
		this.setArguments(args);
		this.setMethod(m);
	}

	/**
	 * 
	 * @return the method
	 */
	public Method getMethod() {
		return method;
	}

	/**
	 * 
	 * @param method the method to set
	 */
	public void setMethod(Method method) {
		this.method = method;
	}

	/**
	 * 
	 * @return the method's arguments
	 */
	public Object[] getArguments() {
		return arguments;
	}

	/**
	 * 
	 * @param arguments the arguments to set
	 */
	public void setArguments(Object[] arguments) {
		this.arguments = arguments;
	}

}
